package com.laozhao.deliveryManager.util;

import java.io.IOException;

import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;

public class PrintExcelByJcom {

	/**
	 * 实线Excel打印预览
	 * 
	 * @param fileName
	 * 
	 */
	public static void printPerview(String fileName) {
		ReleaseManager rm = new ReleaseManager();
		try {
			// 创建可加载的组件
			ExcelApplication excel = new ExcelApplication(rm);
			// 创建工作薄对象
			ExcelWorkbooks xlBooks = excel.Workbooks();
			// 打开工作薄
			ExcelWorkbook xlBook = xlBooks.Open(fileName);
			// 打印
			ExcelWorksheet xlSheet = excel.ActiveSheet();
			// 显示Excel
			excel.Visible(true);
			// 输出页面
			xlSheet.PrintOut(1, 1, 1, true);
			// 关闭
			xlBook.Close(false, null, false);
			excel.Quit();
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			rm.release();
		}
	}
	
	/**
	 * 实线Excel 打印
	 * 
	 * @param fileName
	 * 
	 */
	public static void printExcel(String fileName) throws IOException, JComException {
		ReleaseManager rm = new ReleaseManager();
//		String fileName = null;
//		FileOutputStream fileOut = null;
		ExcelApplication excel = null;
		try {
//			fileName = "";
//			fileOut = new FileOutputStream(fileName);
//			workbook.write(fileOut);
			
			excel = new ExcelApplication(rm);
			ExcelWorkbooks xlBooks = excel.Workbooks();
			ExcelWorkbook xlBook = xlBooks.Open(fileName);
			ExcelWorksheet xlSheet = excel.ActiveSheet();
			
			//call one special printer to print.
			String printer = "";
			Object[] arglist = new Object[7]; 
            arglist[0] = null; 
            arglist[1] = null; 
            arglist[2] = null; 
            arglist[3] = null; 
            arglist[4] = printer;
            arglist[5] = null; 
            arglist[6] = null; 
            xlSheet.method("PrintOut", arglist); 
			
			//call a default printer to print.
			//xlSheet.PrintOut();
			
			xlBook.Close(false, null, false);
			
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			if(excel != null){
				excel.Quit();
			}
			rm.release();
//			fileOut.close();
//			File file = new File(fileName);
//			if (file.exists()) {
//				file.delete();
//			}
		}
	}
}
